package display.tabs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import structs.book.Book;
import structs.quantity.Quantity;
import structs.quantity.QuantityList;
import Util.Util;
import display.InventoryData;

public class InventoryQueryTab extends Tab {
	private InvKeyListener kl;
	private BookInfoTab panel_1;
	private JPanel panel;
	private JLabel lblAuthor;
	private JLabel lblSalePrice;
	private JLabel lblISBN;
	private JTextField textField;
	private JLabel lblTitle;
	private JLabel lblEd;
	private JScrollPane scrollPane;
	private JTable table;
	private DefaultTableModel tableM;

	/**
	 * Create the frame.
	 */
	public InventoryQueryTab(InventoryData i) {
		super(i);
		initialize();
		addContents();
	}
	
	private void initialize() {
		setBorder(new EmptyBorder(5, 5, 5, 5));
	}
	
	private void addContents() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{476, 1, 0};
		gridBagLayout.rowHeights = new int[]{20, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 3.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		panel_1 = new BookInfoTab(inv);
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 0;
		add(panel_1, gbc_panel_1);
		
		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 0, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{49, 126, 0, 86, 33, 20, 12, 150, 0};
		gbl_panel.rowHeights = new int[]{20, 0, 0, 402, 0};
		gbl_panel.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		lblAuthor = new JLabel("Author");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.fill = GridBagConstraints.HORIZONTAL;
		gbc_label.gridwidth = 2;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 3;
		gbc_label.gridy = 0;
		panel.add(lblAuthor, gbc_label);
		
		lblSalePrice = new JLabel("Sale Price");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 6;
		gbc_label_1.gridy = 0;
		panel.add(lblSalePrice, gbc_label_1);
		
		lblISBN = new JLabel("ISBN");
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_label_2.insets = new Insets(0, 0, 5, 5);
		gbc_label_2.gridx = 0;
		gbc_label_2.gridy = 1;
		panel.add(lblISBN, gbc_label_2);
		
		textField = new JTextField();
		textField.setColumns(10);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 1;
		panel.add(textField, gbc_textField);
		
		lblTitle = new JLabel("Title");
		GridBagConstraints gbc_label_3 = new GridBagConstraints();
		gbc_label_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_label_3.gridwidth = 3;
		gbc_label_3.insets = new Insets(0, 0, 5, 5);
		gbc_label_3.gridx = 3;
		gbc_label_3.gridy = 1;
		panel.add(lblTitle, gbc_label_3);
		
		lblEd = new JLabel("Ed");
		GridBagConstraints gbc_label_4 = new GridBagConstraints();
		gbc_label_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_label_4.insets = new Insets(0, 0, 5, 5);
		gbc_label_4.gridx = 6;
		gbc_label_4.gridy = 1;
		panel.add(lblEd, gbc_label_4);
		
		scrollPane = new JScrollPane(table = new JTable());
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 8;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 3;
		panel.add(scrollPane, gbc_scrollPane);
		
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.anchor = GridBagConstraints.NORTHWEST;
		gbc_table.insets = new Insets(0, 0, 5, 0);
		gbc_table.gridwidth = 6;
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 1;
		
		tableM = (DefaultTableModel) table.getModel();
		tableM.setColumnIdentifiers(new String[]{"Qty","Type","Location","Purchase"});
	}
	
	private class InvKeyListener extends KeyAdapter {
		
		@Override
		public void keyPressed(KeyEvent arg0) {
			if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
				Util.printStatusString("You Pressed Enter!");
				
				emptyTable();
				
				String isbn = textField.getText();
				Book b = inv.getBook(isbn);
				
				updateLabels(b);
				
				if (b == null) {
					noQty();
					return;
				}
				
				QuantityList ql = inv.qtyLookup(isbn);
				Util.printStatusString("QuantityList for isbn: "+isbn);
				
				if (ql == null || ql.size() == 0)
					noQty();
				else
					for (int i = 0; i < ql.size(); ++i)
						displayQuantity(ql.get(i));
				
				((DefaultTableModel)table.getModel()).fireTableDataChanged();
			}
		}
		
		private void updateLabels(Book b){
			if (b == null) {
				lblAuthor.setText("Author");
				lblTitle.setText("Title");
				lblEd.setText("Ed");
				lblSalePrice.setText("$0.00");
			}
			else {
				lblAuthor.setText(b.getAuthor());
				lblTitle.setText(b.getTitle());
				lblEd.setText(b.getEdition().getEd());
				lblSalePrice.setText(String.format("$%1$.2f",b.getPrice()));
			}
		}
		
		private void emptyTable() {
			for (int i = 0; i < ((DefaultTableModel)table.getModel()).getRowCount(); )
				((DefaultTableModel)table.getModel()).removeRow(i);
		}
		
		private void displayQuantity(Quantity q) {
			
			String[] tmp = new String[]{
					String.valueOf(q.getQty()),
					q.getType().getType(),
					q.getType().getLocation().toString(),
					String.format("$%1$.2f",q.getType().getPrice())};
			((DefaultTableModel)table.getModel()).addRow(tmp);
			
		}
		
		private void noQty() {
			((DefaultTableModel)table.getModel()).addRow(new String[]{"","None"});
		}
		
	}

}
